using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

using MT.eVotePRS.Common;

namespace MT.eVotePRS.UI
{
    public partial class frmMain : Form
    {
        public frmMain()
        {
            InitializeComponent();

            Program.ArsAPIHelper = MT.eVotePRS.Device.ARSAPIHelper.Instance;
        }

        private void frmMain_Load(object sender, EventArgs e)
        {
            this.Text = string.Format( "{0} {1}" , Constants.AssemblyProduct, Constants.AssemblyVersion);
            hardWareCheck();
        }


        /// <summary>
        /// Hardware check before enter the management functions.
        /// </summary>
        private void hardWareCheck()
        {
            int ret = Program.ArsAPIHelper.RegisterDeviceNotify((Int32)this.Handle);
            if (ret != 1)
                System.Diagnostics.Debug.WriteLine("Register Device Notify ReturnValue=" + ret.ToString());

            int devCount = Program.ArsAPIHelper.ArsDeviceConnected();
            if (devCount < 1)
            {
                DialogBox.boxWarning("Receiver Count=" + devCount.ToString() + ", Please prepare the receiver first.", "Warning!");
            }
        }

        /// <summary>
        /// Lend management form
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnLend_Click(object sender, EventArgs e)
        {
            using (frmCheckOut form = new frmCheckOut())
            {
                form.StartPosition = FormStartPosition.CenterParent;
                form.ShowDialog();
                form.Dispose();
            }
        }

        /// <summary>
        /// Return management form
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnReturn_Click(object sender, EventArgs e)
        {
            using (frmCheckIn form = new frmCheckIn())
            {
                form.StartPosition = FormStartPosition.CenterParent;
                form.ShowDialog();
                form.Dispose();
            }
        }

        /// <summary>
        /// Fail management form
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnFail_Click(object sender, EventArgs e)
        {
            using (frmFail form = new frmFail())
            {
                form.StartPosition = FormStartPosition.CenterParent;
                form.ShowDialog();
                form.Dispose();
            }
        }

        /// <summary>
        /// Lost management form
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnLost_Click(object sender, EventArgs e)
        {
            using (frmLost form = new frmLost())
            {
                form.StartPosition = FormStartPosition.CenterParent;
                form.ShowDialog();
                form.Dispose();
            }
        }

        /// <summary>
        /// Found management form
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnFound_Click(object sender, EventArgs e)
        {
            using (frmHandsetMaint form = new frmHandsetMaint())
            {
                form.StartPosition = FormStartPosition.CenterParent;
                form.ShowDialog();
                form.Dispose();
            }
        }

        /// <summary>
        /// Report management form
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnReport_Click(object sender, EventArgs e)
        {
            int function = 0;
            int functionNo = 0;

            while (true)
            {
                if (function == -1)
                {
                    return;
                }
                if (function == 0)
                {
                    using (frmReportMenu form = new frmReportMenu(functionNo))
                    {
                        form.StartPosition = FormStartPosition.CenterParent;
                        form.ShowDialog();
                        function = form.RetValue;

                        functionNo = function;

                        form.Dispose();
                    }
                }
                else if (function == 2)
                {
                    using (frmRptIndividual form1 = new frmRptIndividual())
                    {
                        form1.StartPosition = FormStartPosition.CenterParent;
                        form1.ShowDialog();
                        form1.Dispose();
                        functionNo = 0;
                        function = 0;
                    }
                }
                else if (function == 1)
                {
                    using (frmReportMenu form1 = new frmReportMenu(functionNo))
                    {
                        form1.StartPosition = FormStartPosition.CenterParent;
                        form1.ShowDialog();
                        form1.Dispose();

                        function = form1.RetValue;
                        functionNo = function;
                    }
                }
                else if (function == 11)
                {
                    using (frmRptInventHandset form1 = new frmRptInventHandset())
                    {
                        form1.StartPosition = FormStartPosition.CenterParent;
                        form1.ShowDialog();
                        form1.Dispose();
                    }
                    function = 1;
                }
                else if (function == 12)
                {
                    using (frmRptInventHandsetEvent form1 = new frmRptInventHandsetEvent())
                    {
                        form1.StartPosition = FormStartPosition.CenterParent;
                        form1.ShowDialog();
                        form1.Dispose();
                    }

                    function = 1;
                }
                else if (function == 13)
                {
                    using (frmRptInventStudent form1 = new frmRptInventStudent())
                    {
                        form1.StartPosition = FormStartPosition.CenterParent;
                        form1.ShowDialog();
                        form1.Dispose();
                    }

                    function = 1;
                }
                else if (function == 14)
                {
                    using (frmRptInventStudentEvent form1 = new frmRptInventStudentEvent())
                    {
                        form1.StartPosition = FormStartPosition.CenterParent;
                        form1.ShowDialog();
                        form1.Dispose();
                    }

                    function = 1;
                }
            }
        }

        /// <summary>
        /// Check management form
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnCheck_Click(object sender, EventArgs e)
        {
            int function = 0;
            
            while (true)
            {
                if (function == 0)
                {
                    using (frmCheckMenu form = new frmCheckMenu())
                    {
                        form.StartPosition = FormStartPosition.CenterParent;
                        form.ShowDialog();

                        function = form.RetValue;

                        form.Dispose();
                    }
                }
                else if (function == 1)
                {
                    using (frmCheck form1 = new frmCheck())
                    {
                        form1.StartPosition = FormStartPosition.CenterParent;
                        form1.ShowDialog();
                        form1.Dispose();
                    }
                    function = 0;
                }
                else if (function == 2)
                {
                    using (frmCheckStudent form1 = new frmCheckStudent())
                    {
                        form1.StartPosition = FormStartPosition.CenterParent;
                        form1.ShowDialog();
                        form1.Dispose();
                    }
                    function = 0;
                }
                else if (function == -1)
                {
                    return;
                }
            }
        }

        private void btnExit_Click(object sender, EventArgs e)
        {
            this.Dispose();
            this.Close();

            Application.Exit();
        }

        private void btnSetting_Click(object sender, EventArgs e)
        {
            using (frmSettingMenu frm = new frmSettingMenu())
            {
                frm.StartPosition = FormStartPosition.CenterParent;
                frm.ShowDialog();
                frm.Dispose();
            }
        }
    }
}